# coding=utf-8
import time;
from FetchSearchHtml import FetchSearchHtml;
from DataProcess import DataProcess;


def appendOfferList(offerList1, offerList2):
    offerList1.extend(offerList2);
    # return list(set(offerList1));
    return offerList1;


class App:
    def __init__(self, keyWord, maxCount, pageIndex=0):
        self.keyWord = keyWord;
        self.pageIndex = pageIndex;
        self.maxCount = maxCount;
        self.count = 0;
        self.offerList = [];
        self.ids = [];

    def nextPage(self):
        self.pageIndex += 1;

    def main(self):
        while (self.count < self.maxCount):
            self.nextPage();
            print("search next page:", self.pageIndex);
            self.fetchSearch();

        print("fetch list size:", self.count);
        dataProcess = DataProcess(self.offerList);
        dataProcess.process()  # 加工和输出cvs

    def fetchSearch(self):
        fetchSearchHtml = FetchSearchHtml(self.keyWord);
        offerDic = fetchSearchHtml.fetchOfferDictionary(self.pageIndex, "");
        html = fetchSearchHtml.fetchHtml(self.pageIndex);
        waterfallCtrPageId = fetchSearchHtml.getWaterfallCtrPageId(html);
        offerDic2 = fetchSearchHtml.fetchOfferDictionary(self.pageIndex, waterfallCtrPageId);

        offerList = appendOfferList(offerDic, offerDic2);

        currentSize = len(self.offerList);
        for item in offerList:
            id = item['id'];
            if id not in self.ids:
                self.ids.append(id);
                self.offerList.append(item);
            # else:
            #     filter_data = [x for x in offerList if x['id'] == id]
            #     print(filter_data)
            #     print(item)
        self.count = len(self.offerList);

        expectCount = currentSize + len(offerList);
        if expectCount > self.count:
            print("去除重复记录:", expectCount - self.count);


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    ticks = time.time()
    app = App("TV+Cart", 10);
    app.main();

    # dict = {'Name': 'Zara', 'Age': 7}

    # print(dict.get('Name'))
    # print(dict.keys()['Age'])
    # print("Value : %s", dict.keys()['fff'])

    print("总用时{}秒".format(int(time.time() - ticks)));

# See PyCharm help at https://www.jetbrains.com/help/pycharm/
